package com.aiyige.page.pay;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.eventbus.EventBuySuccess;
import com.aiyige.page.pay.model.UnpayModel;
import com.aiyige.page.pay.util.PayUtil;
import com.aiyige.track.TrackEvent;
import com.aiyige.utils.DialUtil;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.widget.CommonTitleDetailDialog;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.PayResultPage)
public class PayResultPage extends BaseActivity {
    @BindView(R.id.failBackToHomeBtn)
    TextView failBackToHomeBtn;
    @BindView(R.id.repayBtn)
    TextView repayBtn;
    @BindView(R.id.failLayout)
    View failLayout;
    @BindView(R.id.viewOrderBtn)
    TextView viewOrderBtn;
    @BindView(R.id.successBackToHomeBtn)
    TextView successBackToHomeBtn;
    @BindView(R.id.successLayout)
    View successLayout;
    @BindView(R.id.paySuccessLayout)
    View paySuccessLayout;
    @BindView(R.id.payFailLayout)
    View payFailLayout;
    @Autowired
    int payResult;
    @Autowired
    String orderId;
    @Autowired
    String sellerPhone;
    @Autowired
    String goodsId;
    TrackEvent trackEvent = new TrackEvent();


    @BindView(R.id.fl_controller)
    FrameLayout flController;

    @BindView(R.id.v_pay_result_line)
    View viewLine;

    @BindView(R.id.contactLayout)
    View contactLayout;

    @BindView(R.id.btn_ok)
    TextView tvOk;

    @BindView(R.id.tv_pay_success)
    TextView tvPaySuccess;

    @BindView(R.id.tv_pay_failed)
    TextView tvPayFailed;




    @Autowired
    boolean isServiceChargePay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_activity_pay_result);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        switch (payResult) {
            case PayUtil.PAY_RESULT_FAILED:
                paySuccessLayout.setVisibility(View.INVISIBLE);
                payFailLayout.setVisibility(View.VISIBLE);
                successLayout.setVisibility(View.INVISIBLE);
                failLayout.setVisibility(View.VISIBLE);
                break;
            case PayUtil.PAY_RESULT_SUCCEED:
                trackEvent.sendEvent(this, TrackEvent.PAY_SUCCESS, TrackEvent.PAY_SUCCESS_LABEL);
                EventBus.getDefault().post(EventBuySuccess.newBuilder()
                        .goodsId(goodsId)
                        .orderId(orderId)
                        .build());
                paySuccessLayout.setVisibility(View.VISIBLE);
                payFailLayout.setVisibility(View.INVISIBLE);
                successLayout.setVisibility(View.VISIBLE);
                failLayout.setVisibility(View.INVISIBLE);
                break;
            default:
                ToastX.show(R.string.unknown_error);
                finish();
                break;
        }

        if (isServiceChargePay) {
            viewLine.setVisibility(View.GONE);
            flController.setVisibility(View.GONE);
            contactLayout.setVisibility(View.GONE);
            tvOk.setVisibility(View.VISIBLE);
            if (payResult == PayUtil.PAY_RESULT_FAILED) {
                tvOk.setText(getResources().getString(R.string.repay_money));
                tvPayFailed.setText(getResources().getString(R.string.pay_failed));
            } else {
                tvOk.setText(getResources().getString(R.string.ok));
                tvPaySuccess.setText(getResources().getString(R.string.pay_success));
            }
        }
    }

    @OnClick({R.id.contactLayout, R.id.failBackToHomeBtn, R.id.repayBtn, R.id.viewOrderBtn, R.id.successBackToHomeBtn,R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.contactLayout:
                DialUtil.dial(sellerPhone);
                break;
            case R.id.successBackToHomeBtn:
            case R.id.failBackToHomeBtn:
                finish();
                ARouter.getInstance()
                        .build(ARouterConfig.MainPage)
                        .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                        .navigation(this);
                break;
            case R.id.repayBtn:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.viewOrderBtn:
                ARouter.getInstance()
                        .build(ARouterConfig.OrderDetailPage)
                        .withBoolean("isBuyer", true)
                        .withString("orderId", orderId)
                        .navigation();
                finish();
                break;
            case R.id.btn_ok:
                serviceChargePayCheck();
                break;
        }
    }

    private void serviceChargePayCheck() {
        if (!PayUtil.hasUnpay()) {
            finish();
            return;
        }
        if (!isServiceChargePay) {
            return;
        }
        if (payResult == PayUtil.PAY_RESULT_FAILED) {
            setResult(RESULT_CANCELED);
            finish();
        }
        else if (payResult == PayUtil.PAY_RESULT_SUCCEED) {
            if (PayUtil.hasUnpay()) {
                final UnpayModel unpayModel = PayUtil.getUnpayModel();
                CommonTitleDetailDialog.newInstance(getString(R.string.pay_service_charge),unpayModel.getUserFeeDescription(),null,getString(R.string.to_pay)).setListener(new CommonTitleDetailDialog.Listener() {

                    @Override
                    public void onConfirmClassed() {
                        ARouter.getInstance().build(ARouterConfig.ConfirmPayPage).withParcelable("unpayModel",unpayModel).withBoolean("isServiceChargePay", true).navigation();
                        finish();
                    }
                }).show(getSupportFragmentManager(),"");
            }
        }
    }

    @Override
    public void onBackPressed() {

    }
}
